package com.mingqi.enetconnected.primary.mytask.taskDetails;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.BaseFragment;
import com.mingqi.enetconnected.primary.mytask.activity.PutTableActivity;
import com.mingqi.enetconnected.primary.mytask.adapter.DeviceInfoAdapter;
import com.mingqi.enetconnected.primary.mytask.adapter.GasToolAdapter;
import com.mingqi.enetconnected.primary.mytask.adapter.TableToolAdapter;
import com.mingqi.enetconnected.primary.mytask.event.PutTableEvent;
import com.mingqi.enetconnected.util.DeviceUtils;
import com.mingqi.enetconnected.util.hint.ToastUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by li on 2018/8/13.
 * WeChat 18571658038
 * author LiWei
 */

public class DeviceInfoFragment extends BaseFragment {

    @BindView(R.id.rv_expand_list)
    RecyclerView mRvExpandList;
    private DeviceInfoAdapter mDeviceInfoAdapter;

    @Override
    protected View bindView(ViewGroup container) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_device_info, container, false);
        return view;
    }

    @Override
    protected void initView() {

        List<String> list = new ArrayList<>();

        list.add("表具信息");
        list.add("燃气具");

        mDeviceInfoAdapter = new DeviceInfoAdapter(mContext,list);

        mRvExpandList.setAdapter(mDeviceInfoAdapter);
        mRvExpandList.setLayoutManager(new LinearLayoutManager(mContext));
        mRvExpandList.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                .colorResId(R.color.space)
                .size(DeviceUtils.dp2px(mContext,10))
                .build());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void newPutTable(PutTableEvent event){

        boolean put = event.isNewPut();
        if(put) {

            RecyclerView rvExpand = (RecyclerView) mDeviceInfoAdapter.getViewByPosition(mRvExpandList,0,R.id.rv_expand);
            TableToolAdapter adapter = (TableToolAdapter) rvExpand.getAdapter();

            adapter.addData(0,"");

            int visibility = rvExpand.getVisibility();
            if(visibility == View.GONE) {

                rvExpand.setVisibility(View.VISIBLE);
            }
        } else {

            RecyclerView rvExpandGas = (RecyclerView) mDeviceInfoAdapter.getViewByPosition(mRvExpandList,1,R.id.rv_expand);

            GasToolAdapter gasAdapter = (GasToolAdapter) rvExpandGas.getAdapter();
            gasAdapter.addData(0,"");

            int visibility = rvExpandGas.getVisibility();
            if(visibility == View.GONE) {

                rvExpandGas.setVisibility(View.VISIBLE);
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        EventBus.getDefault().unregister(this);
    }
}
